<template>
	<el-container>
		<el-aside width="300px" v-loading="showDicloading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="dicFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree show-checkbox ref="dic" class="menu" node-key="id" :default-expand-all="true" :data="dicList" :props="dicProps" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="dicFilterNode" @node-click="dicClick">
						<template #default="{node, data}">
							<span class="custom-tree-node">
								<span class="label">{{ data.display_name }}</span>
								<span class="code">{{ node.name }}{{ data.name }}</span>
							</span>
						</template>
					</el-tree>
				</el-main>
				<el-footer style="height:51px;">
					<el-button type="primary" size="small" style="width: 100%;">菜单</el-button>
				</el-footer>
			</el-container>
		</el-aside>
		<el-container class="is-vertical">
			<el-main class="nopadding">
				<scTable stripe :default-expand-all="true" ref="table" :apiObj="listApi" row-key="id" :params="listApiParams" @selection-change="selectionChange" :paginationLayout="'prev, pager, next'">
					<el-table-column>
						<template #header>
		          <el-button type="primary" plain>全选</el-button>
		        </template>
					  <template #default="scope">
					  	<el-card shadow="never" :header="scope.row.display_name">
						    <el-form label-width="180px" label-position="left" inline>
						      <el-form-item>
						      	<template v-for="(item,index) in scope.row.permissions" :key="index">
						      			<el-checkbox>{{item.display_name}}</el-checkbox>
						      	</template>
									</el-form-item>
						    </el-form>
					  	</el-card>
					  </template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>

</template>

<script>

	import Sortable from 'sortablejs'

	export default {
		name: 'permission',
		components: {

		},
		data() {
			return {
				role_id:null,
				dialog: {
					dic: false,
					info: false
				},
				mode:'add',
				showDicloading: true,
				dicList: [],
				dicFilterText: '',
				dicProps: {
					label: 'name'
				},
				listApi: null,
				listApiParams: {},
				selection: []
			}
		},
		watch: {
			dicFilterText(val) {
				this.$refs.dic.filter(val);
			}
		},
		mounted() {
			this.getDic()
			this.rowDrop()
		},
		methods: {
			validate(cb){
				cb(this.$refs.table.tableData);
			},
			setData(row){

			},
			open(role_id){
				this.showDicloading = false;
				// this.role_id = role_id;
				this.getDic()
				this.rowDrop()
				return this;
			},
			//加载树数据
			async getDic(){
				var res = await this.$API.system.menu.tree.get();
				this.showDicloading = false;
				this.dicList = res.data;
				//获取第一个节点,设置选中 & 加载明细列表
				var firstNode = this.dicList[0];
				if(firstNode){
					this.$nextTick(() => {
						// this.$refs.dic.setCurrentKey(firstNode.id)
					})
					this.listApiParams = {
						path: firstNode.path,
						id: firstNode.id,
						role_id: this.role_id
					}
					this.listApi = this.$API.system.permission.list;
				}
			},
			//树过滤
			dicFilterNode(value, data){
				if (!value) return true;
				var targetText = data.name + data.code;
				return targetText.indexOf(value) !== -1;
			},
			//树点击事件
			dicClick(data){
				this.$refs.table.reload({
					path: data.path,
					id: data.id,
					role_id: this.role_id
				})
			},
			//行拖拽
			rowDrop(){
				const _this = this
				const tbody = this.$refs.table.$el.querySelector('.el-table__body-wrapper tbody')
				Sortable.create(tbody, {
					handle: ".move",
					animation: 300,
					ghostClass: "ghost",
					onEnd({ newIndex, oldIndex }) {
						const tableData = _this.$refs.table.tableData
						const currRow = tableData.splice(oldIndex, 1)[0]
						tableData.splice(newIndex, 0, currRow)
						_this.$message.success("排序成功")
					}
				})
			},
			//提交明细
			saveList(){
				this.$refs.listDialog.submit(async (formData) => {
					this.isListSaveing = true;
					var res = await this.$API.demo.post.post(formData);
					this.isListSaveing = false;
					if(res.code == 0){
						//这里选择刷新整个表格 OR 插入/编辑现有表格数据
						this.listDialogVisible = false;
						this.$message.success("操作成功")
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//表格内开关事件
			changeSwitch(val, row){

				//1.还原数据
				row.checked = row.checked == 1 ? 0: 1
				//2.执行加载
				row.$switch_checked = true;
				//3.等待接口返回后改变值
				setTimeout(()=>{
					delete row.$switch_checked;
					row.checked = val;
					// this.$message.success(`操作成功id:${row.id} val:${val}`)
				}, 0)
			},
			//本地更新数据
			handleDicSuccess(data, mode){
				if(mode=='add'){
					data.id = new Date().getTime()
					if(this.dicList.length > 0){
						this.$refs.table.upData({
							code: data.code
						})
					}else{
						this.listApiParams = {
							code: data.code
						}
						this.listApi = this.$API.dic.info;
					}
					this.$refs.dic.append(data, data.parentId[0])
					this.$refs.dic.setCurrentKey(data.id)
				}else if(mode=='edit'){
					var editNode = this.$refs.dic.getNode(data.id);
					//判断是否移动？
					var editNodeParentId =  editNode.level==1?undefined:editNode.parent.data.id
					if(editNodeParentId != data.parentId){
						var obj = editNode.data;
						this.$refs.dic.remove(data.id)
						this.$refs.dic.append(obj, data.parentId[0])
					}
					Object.assign(editNode.data, data)
				}
			},
			//本地更新数据
			handleListSuccess(data, mode){
				if(mode=='add'){
					data.id = new Date().getTime()
					this.$refs.table.tableData.push(data)
				}else if(mode=='edit'){
					this.$refs.table.tableData.filter(item => item.id===data.id ).forEach(item => {
						Object.assign(item, data)
					})
				}
			}
		}
	}
</script>

<style scoped>
	.menu:deep(.el-tree-node__label) {display: flex;flex: 1;height:100%;}
	.custom-tree-node {display: flex;flex: 1;align-items: center;justify-content: space-between;font-size: 14px;padding-right: 24px;height:100%;}
	.custom-tree-node .code {font-size: 12px;color: #999;}
	.custom-tree-node .do {display: none;}
	.custom-tree-node:hover .code {display: none;}
	.custom-tree-node:hover .do {display: inline-block;}
</style>
